<template>
<div>
  <el-row>
    <el-col :span="2" class="height40">
      选择车辆:
    </el-col>
    <el-col :span="6">
      <el-select v-model="truck.truckId" placeholder="请选择" @change="selectTruck">
        <el-option v-for="truck in waitTrucks" :key="truck.truckId" :label="truck.truckNumber" :value="truck.truckId">
        </el-option>
      </el-select>
    </el-col>
    <el-col :span="1" class="height40">
      司机:
    </el-col>
    <el-col :span="2" class="height40">
      {{truck.emps.empName}}
    </el-col>
    <el-col :span="2" :offset="1" class="height40">
      车辆类型:
    </el-col>
    <el-col :span="2" class="height40">
      {{truck.truckType}}
    </el-col>
    <el-col :span="1" :offset="5">
      <el-button type="primary" icon="el-icon-truck" @click="receive">车辆已到达</el-button>
    </el-col>
  </el-row>
  <el-table
    :data="cargos"
    border
    height="500"
    style="width: 100%; margin-top: 20px">
    <el-table-column prop="docId" label="单号"></el-table-column>
    <el-table-column prop="cargoName" label="名称"></el-table-column>
    <el-table-column prop="num" label="数量/件"></el-table-column>
    <el-table-column prop="weight" label="重量/kg"></el-table-column>
    <el-table-column prop="bulk" label="体积/m³"></el-table-column>
    <el-table-column prop="endSite" label="终点站"></el-table-column>
    <el-table-column prop="state" label="运单状态"></el-table-column>
  </el-table>
</div>
</template>

<script>
import {getWaitTruck,getDocByDispa,updTruck} from 'network/truck.js'
import {listDocReceive} from 'network/document.js'


let loginAccount = JSON.parse(sessionStorage.loginAccount)

export default {
  data() {
    return {
      waitTrucks:[], //站点所有待接收车辆
      truck: {
        truckId: '',
        truckState: '',
        emps: {empName: ''}
      },
      documents: [],
      cargos: []
    }
  },
  methods: {
    receive() {
      if(this.truck.truckNumber == null) {
        return false
      }
      this.$confirm(this.truck.truckNumber+'确认已到达?','确认后货物全部入库', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let truck = {truckId: this.truck.truckId,
          empId: this.truck.empId,
          truckState: '待命',
          nowSite: loginAccount.sitetb.siteName,
          endSite: '0',
        }
        updTruck(truck).then(() => {
            getWaitTruck(loginAccount.sitetb.siteName).then(res => {
              this.waitTrucks = res
            })
        })
        listDocReceive({documents: this.documents,siteName: loginAccount.sitetb.siteName}).then(res => {
          if(res == "success") {
            //更新当前网点运单
            this.$store.dispatch('setDocument',loginAccount.sitetb.siteName)
          }
        })
        //重置当前页面数据
        this.truck = {truckId: '',truckState: '',emps: {empName: ''}}
        this.documents.splice(0)
        this.cargos.splice(0)
        this.$message({
          type: 'success',
          message: '接收成功,所有货物已入库!'
        });
      })
    },
    selectTruck(value) {
      this.truck = this.waitTrucks.find(truck => truck.truckId == value)
      getDocByDispa(this.truck.truckId).then(res => {
        this.documents.splice(0)
        this.cargos.splice(0)
        for(let doc of res) {
          this.documents.push(doc.document)
          for(let cargo of doc.document.cargos) {
            this.cargos.push({
              cargoId: cargo.cargoId,
              docId: cargo.docId,
              cargoName: cargo.cargoName,
              cargoPack: cargo.cargoPack,
              num: cargo.num,
              bulk: cargo.bulk,
              weight: cargo.weight,
              price: cargo.price,
              endSite: doc.document.docEnd,
              state: doc.document.state
            })
          }
        }
      })
    }
  },
  mounted() {
    getWaitTruck(loginAccount.sitetb.siteName).then(res => {
      this.waitTrucks = res
    })
  }
}
</script>

<style scoped>
</style>

